package com.wst.system.util;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Classname FileUtils
 * @Description TODO
 * @Date 2023/9/17 13:17
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class FileUtils {


    public static List<File> getFiles(String path){
        File file = new File(path);
        if(!file.exists()){
            return Collections.emptyList();
        }
        List<File> fileLists = new ArrayList<>();
        if(file.isDirectory()){
            File[] files = file.listFiles();
            for (File f : files) {
                if(f.isDirectory()){
                    getFiles(fileLists,f.listFiles());
                }else {
                    fileLists.add(f);
                }
            }
        }else {
            fileLists.add(file);
        }
        return fileLists;
    }


    public static String getFileType(File file){
        String fileName = file.getName();
        int dotIndex = fileName.lastIndexOf(".");
        if (dotIndex != -1 && dotIndex < fileName.length() - 1) {
            String fileType = fileName.substring(dotIndex + 1);
            System.out.println("文件类型为: " + fileType);
            return fileType;
        } else {
            System.out.println("无法获取文件类型");
            return fileName;
        }
    }

    public static String getFileType(String fileName){
        int dotIndex = fileName.lastIndexOf(".");
        if (dotIndex != -1 && dotIndex < fileName.length() - 1) {
            String fileType = fileName.substring(dotIndex + 1);
            System.out.println("文件类型为: " + fileType);
            return fileType;
        } else {
            System.out.println("无法获取文件类型");
            return fileName;
        }
    }

    private static void getFiles(List<File> fileList,File[] files){
        for (File f : files) {
            if(f.isDirectory()){
                getFiles(fileList,f.listFiles());
            }else {
                fileList.add(f);
            }
        }
    }

    public static byte[] getUploadFileByPath(String path){
        File file=new File(path);
        FileInputStream inputStream=null;
        ByteArrayOutputStream baos=null;
        try {
            inputStream=new FileInputStream(file);
            baos = new ByteArrayOutputStream();
            byte buff[] = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buff)) != -1) {
                baos.write(buff, 0, len);
            }
            baos.flush();
            byte[] bytes= baos.toByteArray();
            return bytes;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(baos!=null){
                try {
                    baos.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static byte[] getUploadFileByPath(String path,String fileName) throws FileNotFoundException {
        File file=FileUtils.getFile(path,fileName);
        FileInputStream inputStream=null;
        ByteArrayOutputStream baos=null;
        try {
            inputStream=new FileInputStream(file);
            baos = new ByteArrayOutputStream();
            byte buff[] = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buff)) != -1) {
                baos.write(buff, 0, len);
            }
            baos.flush();
            byte[] bytes= baos.toByteArray();
            return bytes;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(baos!=null){
                try {
                    baos.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static String formatFileSize(long sizeInBytes) {
        if (sizeInBytes < 1024) {
            // 小于 1 KB
            return sizeInBytes + " B";
        } else if (sizeInBytes < 1024 * 1024) {
            // 小于 1 MB
            return String.format("%.2f KB", sizeInBytes / 1024.0);
        } else if (sizeInBytes < 1024 * 1024 * 1024) {
            // 小于 1 GB
            return String.format("%.2f MB", sizeInBytes / (1024.0 * 1024.0));
        } else {
            // 大于等于 1 GB
            return String.format("%.2f GB", sizeInBytes / (1024.0 * 1024.0 * 1024.0));
        }
    }

    public static void outFileByPath(byte[] bytes, String path){
        File file1=new File(path);
        File parentPath=file1.getParentFile();
        if(!parentPath.exists()){
            parentPath.mkdirs();
        }
        InputStream in=null;
        FileOutputStream fos =null;
        try {
            in=new ByteArrayInputStream(bytes);
            fos=new FileOutputStream(file1);
            byte[] b = new byte[1024];
            while ((in.read(b)) != -1) {
                // 写入数据
                fos.write(b);
            }
            fos.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(fos!=null){
                try {
                    fos.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(in!=null){
                try {
                    in.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

    public static void donload(byte[] data, String fileName,HttpServletResponse response){
        OutputStream outputStream=null;
        InputStream in=null;
        try {
            in=new ByteArrayInputStream(data);
            outputStream=response.getOutputStream();
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName, "utf-8"));
            byte[] b = new byte[1024];
            while ((in.read(b)) != -1) {
                // 写入数据
                outputStream.write(b);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(outputStream!=null){
                try {
                    outputStream.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(in != null){
                try {
                    in.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static File getFile(String path,String fileName) throws FileNotFoundException {
        File file=new File(path+File.separator+fileName);
        if(!file.exists()){
            throw new FileNotFoundException(path+" 文件不存在");
        }
        return file;
    }
}
